package com.dongzili.easysearch.common.es;

import cn.hutool.extra.spring.SpringUtil;
import org.dromara.easyes.core.kernel.BaseEsMapper;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EsUtils {

    private final static Map<Class<?>, BaseEsMapper<?>> MAPPER_MAP = new ConcurrentHashMap<>();

    @SuppressWarnings("unchecked")
    public static <T> BaseEsMapper<T> getMapper(final Class<T> t) {
        if (MAPPER_MAP.containsKey(t)){
            return (BaseEsMapper<T>) MAPPER_MAP.get(t);
        }
        BaseEsMapper<?> mapper = SpringUtil.getBean(getBeanName(t) + "AutoEsMapper");
        MAPPER_MAP.put(t, mapper);
        return (BaseEsMapper<T>)mapper;
    }

    /**
     * 获取bean名称
     * @param t
     * @return
     */
    private static <T> String getBeanName(final Class<T> t) {
        return t.getSimpleName().substring(0, 1).toLowerCase() + t.getSimpleName().substring(1);
    }

}
